package com.love_weinuo.ui.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lzy.okgo.callback.StringCallback;
import com.love_weinuo.R;
import com.love_weinuo.bean.MyOrderChangeCallbackBean;
import com.love_weinuo.bean.UserInfoCallBack;
import com.love_weinuo.databinding.FragmentMyselfBinding;
import com.love_weinuo.ui.activity.MyCommissionActivity;
import com.love_weinuo.ui.activity.MyIdeaActivity;
import com.love_weinuo.ui.activity.MyInfoActivity;
import com.love_weinuo.ui.activity.MyOrderActivity;
import com.love_weinuo.ui.activity.PindanListActivity;
import com.love_weinuo.ui.activity.SettingActivity;
import com.love_weinuo.ui.activity.ShowLandActivity;
import com.love_weinuo.util.GsonUtil;
import com.love_weinuo.util.HTTPAPI;
import com.love_weinuo.util.LogUtil;
import com.love_weinuo.util.SpUtils;
import com.love_weinuo.util.ToastUtil;
import com.love_weinuo.util.glideutil.GlideUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

import static com.love_weinuo.bean.Constants.BASE_RES_HEAD_URL;

/**
 * Created by Administrator
 * on 2019/2/21.
 */

public class MySelfFragment extends BaseLazyFragment {

    FragmentMyselfBinding binding;
    String userId = (String) SpUtils.get("module_user_id", "");
    List<MyOrderChangeCallbackBean.ResultEntity.ResEntity> strs = new ArrayList<>();
    List<MyOrderChangeCallbackBean.ResultEntity.ResEntity> pindanList = new ArrayList<>();
    List<MyOrderChangeCallbackBean.ResultEntity.ResEntity> waitList = new ArrayList<>();
    List<MyOrderChangeCallbackBean.ResultEntity.ResEntity> workList = new ArrayList<>();
    List<MyOrderChangeCallbackBean.ResultEntity.ResEntity> goneList = new ArrayList<>();
    List<MyOrderChangeCallbackBean.ResultEntity.ResEntity> waitPayList = new ArrayList<>();

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(LayoutInflater.from(getActivity()), R.layout.fragment_myself, container, false);
        binding.setFragment(this);
        return binding.getRoot();
    }

    @Override
    protected void onVisible() {

    }

    @Override
    public void onResume() {
        super.onResume();
        getReasionAllInfo();
    }

    @Override
    protected void lazyLoad() {
        binding.rightImage.setOnClickListener(this);
        binding.tvModuleSeeAllOrder.setOnClickListener(this);
        binding.llModuleMyLand.setOnClickListener(this);
        binding.llModuleMyCommission.setOnClickListener(this);
        binding.llModuleMyIdea.setOnClickListener(this);
        binding.llModuleSet.setOnClickListener(this);
        binding.head.setOnClickListener(this);
        binding.llModuleGone.setOnClickListener(this);
        binding.llModuleWater.setOnClickListener(this);
        binding.llModuleWork.setOnClickListener(this);
        binding.llModuleWaitPay.setOnClickListener(this);
        binding.tvModuleName.setOnClickListener(this);
        binding.tvModulePhone.setOnClickListener(this);
        binding.ivModuleSex.setOnClickListener(this);
    }

    @Override
    public int setLayoutId() {
        return R.layout.fragment_index;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvModuleName:
                startActivity(new Intent(getActivity(), MyInfoActivity.class));
                break;
            case R.id.tvModulePhone:
                startActivity(new Intent(getActivity(), MyInfoActivity.class));
                break;
            case R.id.ivModuleSex:
                startActivity(new Intent(getActivity(), MyInfoActivity.class));
                break;
            case R.id.head:
                startActivity(new Intent(getActivity(), MyInfoActivity.class));
                break;
            case R.id.rightImage:
                startActivity(new Intent(getActivity(), MyInfoActivity.class));
                break;
            case R.id.tvModuleSeeAllOrder:
                startActivity(new Intent(getActivity(), MyOrderActivity.class));
                break;
            case R.id.llModuleMyLand:
                startActivity(new Intent(getActivity(), ShowLandActivity.class));
                break;
            case R.id.llModuleMyCommission:
                startActivity(new Intent(getActivity(), MyCommissionActivity.class));
                break;
            case R.id.llModuleMyIdea:
                startActivity(new Intent(getActivity(), MyIdeaActivity.class));
                break;
            case R.id.llModuleSet:
                startActivity(new Intent(getActivity(), SettingActivity.class));
                break;
            case R.id.llModuleWater:
                startActivity(new Intent(getActivity(), PindanListActivity.class).putExtra("module_title_name", "待拼单"));
                break;
            case R.id.llModuleWork:
                startActivity(new Intent(getActivity(), PindanListActivity.class).putExtra("module_title_name", "待作业"));
                break;
            case R.id.llModuleGone:
                startActivity(new Intent(getActivity(), PindanListActivity.class).putExtra("module_title_name", "已完成"));
                break;
            case R.id.llModuleWaitPay:
                startActivity(new Intent(getActivity(), PindanListActivity.class).putExtra("module_title_name", "待付款"));
                break;
            default:
        }
    }

    public void getReasionAllInfo() {
        HTTPAPI.getInstance().getUserInfo(userId, new StringCallback() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onSuccess(String s, Call call, Response response) {
                LogUtil.e("获取信息的ID-->" + userId);
                LogUtil.e("个人信息获取成功" + s);
                UserInfoCallBack bean = GsonUtil.GsonToBean(s, UserInfoCallBack.class);
                switch (bean.getErrcode()) {
                    case 0:
                        GlideUtil.setCircleMethod(getActivity(), bean.getResult().getHeadimg(), binding.head);
                        if (0 == bean.getResult().getSex()) {
                            GlideUtil.setResourceMethod(getActivity(), R.drawable.icon_man, binding.ivModuleSex);
                        } else if (1 == bean.getResult().getSex()) {
                            GlideUtil.setResourceMethod(getActivity(), R.drawable.icon_woman, binding.ivModuleSex);
                        }
                        binding.tvModuleName.setText(bean.getResult().getNickname());
                        binding.tvModulePhone.setText(bean.getResult().getMobile());
                        binding.tvModuleAllOrder.setText(bean.getResult().getOrdercount() + "");
                        binding.tvModuleAllPack.setText(bean.getResult().getRemainmoney() + "");
                        getAllPindan();
                        break;
                    default:
                }
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                LogUtil.e("获取个人信息失败" + e.getMessage());
            }
        });
    }

    public void getAllPindan() {
        HTTPAPI.getInstance().getAllMyOrder(userId, new StringCallback() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onSuccess(String s, Call call, Response response) {
                LogUtil.e("拼单获取成功" + s);
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(s);
                    int errorCode = jsonObject.getInt("errcode");
                    if (errorCode == 1) {
                        ToastUtil.showToast(jsonObject.getString("msg"));
                        return;
                    }
                    MyOrderChangeCallbackBean bean = GsonUtil.GsonToBean(s, MyOrderChangeCallbackBean.class);
                    pindanList.clear();
                    pindanList.addAll(bean.getResult().getRes());
                    //0 待拼单 1待付定金 2 代付付款 3  待作业 4 已完成
                    waitList.clear();
                    waitPayList.clear();
                    workList.clear();
                    goneList.clear();
                    for (MyOrderChangeCallbackBean.ResultEntity.ResEntity entity : pindanList) {
                        switch (entity.getOrderstate()) {
                            case "0":
                                waitList.add(entity);
                                break;
                            case "2":
                                waitPayList.add(entity);
                                break;
                            case "3":
                                workList.add(entity);
                                break;
                            case "4":
                                goneList.add(entity);
                                break;
                            default:
                        }

                        if (waitList.size() != 0) {
                            binding.tvModuleWaitPindanNum.setText(waitList.size() + "条");
                            binding.tvModuleWaitPindanNum.setVisibility(View.VISIBLE);
                        }else{
                            binding.tvModuleWaitPindanNum.setVisibility(View.GONE);
                        }

                        if (waitPayList.size() != 0) {
                            binding.tvModuleWaitPayPindanNum.setText(waitPayList.size() + "条");
                            binding.tvModuleWaitPayPindanNum.setVisibility(View.VISIBLE);
                        }

                        if (workList.size() != 0) {
                            binding.tvModuleWorkPindanNum.setText(workList.size() + "条");
                            binding.tvModuleWorkPindanNum.setVisibility(View.VISIBLE);
                        }

                        if (goneList.size() != 0) {
                            binding.tvModuleHaveDonePindanNum.setText(waitList.size() + "条");
                            binding.tvModuleHaveDonePindanNum.setVisibility(View.VISIBLE);
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                LogUtil.e("获取失败" + e.getMessage());
            }
        });
    }

}